local ruyi = fk.CreateSkill {

  name = "joy__ruyi",

  tags = { Skill.Compulsory, },

}



ruyi:addEffect(fk.TurnStart, {
  name = "joy__ruyi",
  frequency = Skill.Compulsory,
  anim_type = "offensive",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return target == player and player:hasSkill(ruyi.name) and not player:getEquipment(Card.SubtypeWeapon)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local _, ret = room:askForUseActiveSkill(player, "joy__ruyi_change", "选择【如意金箍棒】的攻击范围。", true)
    local n
    if _ and ret then
      n = ret.interaction
    if n and n > 0 then
      event:setCostData(self, n)
      return true
    end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player,"@joy_ruyi",event:getCostData(self))
  end,
})
ruyi:addEffect(fk.CardUsing, {
  refresh_events = {fk.CardUsing},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(ruyi.name) and
      data.card.trueName == "slash" and  not player:getEquipment(Card.SubtypeWeapon)
  end,
  on_refresh = function(self, event, target, player, data)
    player:broadcastSkillInvoke(ruyi.name)
    if player:getMark("@joy_ruyi") <= 1 then
      player.room:broadcastPlaySound("./packages/mobile/audio/card/ex_crossbow")
    else
      player.room:broadcastPlaySound("./packages/mobile/audio/card/ex_vineburn")
    end
    player.room:doAnimate("InvokeSkill", {
      name = "金箍棒",
      player = player.id,
      skill_type = "offensive",
    })
  end,
})

ruyi:addEffect("atkrange", {
  name = "#joy__ruyi_attackrange",
  fixed_func = function (self, player)
    if player:hasSkill(ruyi.name) and player:getMark("@joy_ruyi") ~= 0 then
      return player:getMark("@joy_ruyi")
    end
  end,
})

ruyi:addEffect("targetmod", {
  name = "#joy__ruyi_targetmod",
  bypass_times = function(self, player, skill, scope)
    if player:hasSkill(ruyi.name) and not player:getEquipment(Card.SubtypeWeapon)  then 
      return player:hasSkill(ruyi.name) and player:getMark("@joy_ruyi") <= 1 and skill.trueName == "slash_skill" and scope == Player.HistoryPhase
    end
  end,
})

ruyi:addEffect(fk.AfterCardUseDeclared, {
  name = "#joy__ruyi_trigger",
  mute = true,
  events = {fk.AfterCardUseDeclared, fk.AfterCardTargetDeclared,fk.EventAcquireSkill},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(ruyi.name) or player:getEquipment(Card.SubtypeWeapon) ~= nil then return false end
    if event == fk.EventAcquireSkill then
      return data == ruyi and target == player 
    end
    if player == target and player:hasSkill(ruyi.name) and data.card.trueName == "slash" then
      if event == fk.AfterCardUseDeclared then
        if player:getMark("@joy_ruyi") == 2 then
        local n=0
        player.room.logic:getEventsOfScope(GameEvent.UseCard,2, function (e)
          local use = e.data
          if use.from and use.from==player.id and use.card.trueName == "slash"  then
            n=n+1
          end
          return false
        end, Player.HistoryTurn)
        return n == 1  
        else
          return player:getMark("@joy_ruyi") == 3 
        end
      else
        return player:getMark("@joy_ruyi") == 4 and #data:getUseExtraTargets() > 0
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not player:hasSkill(ruyi.name) or player:getEquipment(Card.SubtypeWeapon) ~= nil then return false end
    if event == fk.EventAcquireSkill  then
      room:setPlayerMark(player, "@joy_ruyi", 1)
    else
      if player:getMark("@joy_ruyi") == 2 then
        data.additionalDamage = (data.additionalDamage or 0) + 1
      elseif player:getMark("@joy_ruyi") == 3 then
        data.disresponsiveList = table.map(room.alive_players, Util.IdMapper)
      else
        local to = room:askForChoosePlayers(player, data:getUseExtraTargets(),
        1, 1, "#ruyi-choose:::"..data.card:toLogString(), ruyi.name, true)
        if #to > 0 then
          table.insert(data.tos, to)
        end
      end
    end
  end,
})
ruyi:addEffect(fk.AfterCardTargetDeclared, {
  name = "#joy__ruyi_trigger",
  mute = true,
  events = {fk.AfterCardUseDeclared, fk.AfterCardTargetDeclared,fk.EventAcquireSkill},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(ruyi.name) or player:getEquipment(Card.SubtypeWeapon) ~= nil then return false end
    if event == fk.EventAcquireSkill then
      return data == ruyi and target == player 
    end
    if player == target and player:hasSkill(ruyi.name) and data.card.trueName == "slash" then
      if event == fk.AfterCardUseDeclared then
        if player:getMark("@joy_ruyi") == 2 then
        local n=0
        player.room.logic:getEventsOfScope(GameEvent.UseCard,2, function (e)
          local use = e.data
          if use.from and use.from==player.id and use.card.trueName == "slash"  then
            n=n+1
          end
          return false
        end, Player.HistoryTurn)
        return n == 1  
        else
          return player:getMark("@joy_ruyi") == 3 
        end
      else
        return player:getMark("@joy_ruyi") == 4 and #data:getUseExtraTargets() > 0
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not player:hasSkill(ruyi.name) or player:getEquipment(Card.SubtypeWeapon) ~= nil then return false end
    if event == fk.EventAcquireSkill  then
      room:setPlayerMark(player, "@joy_ruyi", 1)
    else
      if player:getMark("@joy_ruyi") == 2 then
        data.additionalDamage = (data.additionalDamage or 0) + 1
      elseif player:getMark("@joy_ruyi") == 3 then
        data.disresponsiveList = table.map(room.alive_players, Util.IdMapper)
      else
        local to = room:askForChoosePlayers(player, data:getUseExtraTargets(),
        1, 1, "#ruyi-choose:::"..data.card:toLogString(), ruyi.name, true)
        if #to > 0 then
          table.insert(data.tos, to)
        end
      end
    end
  end,
})
ruyi:addEffect(fk.EventAcquireSkill, {
  name = "#joy__ruyi_trigger",
  mute = true,
  events = {fk.AfterCardUseDeclared, fk.AfterCardTargetDeclared,fk.EventAcquireSkill},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(ruyi.name) or player:getEquipment(Card.SubtypeWeapon) ~= nil then return false end
    if event == fk.EventAcquireSkill then
      return data == ruyi and target == player 
    end
    if player == target and player:hasSkill(ruyi.name) and data.card.trueName == "slash" then
      if event == fk.AfterCardUseDeclared then
        if player:getMark("@joy_ruyi") == 2 then
        local n=0
        player.room.logic:getEventsOfScope(GameEvent.UseCard,2, function (e)
          local use = e.data
          if use.from and use.from==player.id and use.card.trueName == "slash"  then
            n=n+1
          end
          return false
        end, Player.HistoryTurn)
        return n == 1  
        else
          return player:getMark("@joy_ruyi") == 3 
        end
      else
        return player:getMark("@joy_ruyi") == 4 and #data:getUseExtraTargets() > 0
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not player:hasSkill(ruyi.name) or player:getEquipment(Card.SubtypeWeapon) ~= nil then return false end
    if event == fk.EventAcquireSkill  then
      room:setPlayerMark(player, "@joy_ruyi", 1)
    else
      if player:getMark("@joy_ruyi") == 2 then
        data.additionalDamage = (data.additionalDamage or 0) + 1
      elseif player:getMark("@joy_ruyi") == 3 then
        data.disresponsiveList = table.map(room.alive_players, Util.IdMapper)
      else
        local to = room:askForChoosePlayers(player, data:getUseExtraTargets(),
        1, 1, "#ruyi-choose:::"..data.card:toLogString(), ruyi.name, true)
        if #to > 0 then
          table.insert(data.tos, to)
        end
      end
    end
  end,
})

return ruyi